package com.example.selfservicestation.util

import android.content.Context
import android.os.Environment
import com.google.gson.Gson
import java.io.Closeable
import java.io.File


val gson = Gson()

/**
 * 虚拟机相对时间（秒）
 */
fun jvmSeconds() : Long {
    return System.nanoTime() / 1000_000_000
}

/**
 * 当前时间（秒）
 */
fun nowSeconds() : Long {
    return System.currentTimeMillis() / 1000
}


val EXT_ROOT_PATH : String = Environment.getExternalStorageDirectory().absolutePath

fun extFile(path: String) : File {
    return File(EXT_ROOT_PATH + File.separator + "refuse-class" + File.separator + path)
}

fun fileRootPath() : String {
    return EXT_ROOT_PATH + File.separator + "refuse-class"
}


fun Closeable.closeQuietly() {
    try {
        close()
    } catch (rethrown: RuntimeException) {
        throw rethrown
    } catch (_: Exception) {
    }
}

fun dp2px(context: Context, dpValue: Float): Int {
    val scale: Float = context.resources.displayMetrics.density
    return (dpValue * scale + 0.5f).toInt()
}

fun px2dp(context: Context, pxValue: Float): Int {
    val scale: Float = context.resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}